function Param(closePrice){
    this.date = new Array();
    this.data = new Array();
    this.barData = new Array();
    this.average = Array();
    this.closePrice = closePrice;
    this.max = this.closePrice * (1 + 0.1);
    this.min = this.closePrice * (1 - 0.1);
    this.interval = 0;
}

 var param = new Param(45.66);
 

function initDate() {
    var length = 0;
    for (var i = 9; i < 15; i++) {
        for (var j = 0; j < 60; j++) {
            if (!(i == 9 && j < 30 || i == 11 && j >= 30 || i == 12)) {
                if (i == 13 && j == 00) {
                    param.date[length++] = "11:30/13:00"
                } else {
                    var hours = i;
                    var minutes = j < 10 ? "0" + j : j;
                    param.date[length++] = hours + ":" + minutes;
                }
            }
        }
    }
    param.date[length] = "15:00";
}


function initData(){
    for(var i in param.date){
        param.data[i] = "-";
    }
}


function getYAxis() {
    param.interval = getInterval().toFixed(3) * 1;
    param.max = (param.closePrice + param.interval).toFixed(3) * 1;
    param.min = (param.closePrice - param.interval).toFixed(3) * 1;
}

function getInterval() {
    if (Math.abs(param.max - param.closePrice) - Math.abs(param.min - param.closePrice)) {
        return Math.abs(param.max - param.closePrice);
    } else {
        return Math.abs(param.min - param.closePrice);
    }
}



function formakData(result) {
    var markets = result.data.markets;
    for (var i in param.data) {
        for (var key in markets[i]) {
                var value = markets[i][key].currentPrice;
                minAndMax(i,value);
                param.data[i] = value;
        }
    }
    param.barData = param.data;
}

function getAverage() {
    var sum = 0
    for (var i = 0; i < param.data.length; i++) {
        if (param.data[i] != "-") {
            sum += param.data[i];
            param.average[i] = sum / (i + 1)
        } else {
            param.average[i] = "-";
        }
    }
}

function minAndMax(i, value) {
    if (0 == i){
        param.min = value;
        param.max = value;
    }
    if (param.min > value) {
        param.min = value;
    };
    if (param.max < value) {
        param.max = value;
    };
}


var myChart = echarts.init(document.getElementById('map'));
myChart.showLoading();

$.getJSON("js/time/data_time.json",function(result){
    initDate();
    initData();
    formakData(result);
    getAverage();
    getYAxis();
    myChart.setOption(loadOption(param));
    myChart.hideLoading();
});


